﻿using System;
using System.Data.SqlClient;
using System.Runtime.Serialization;
using System.Text;
using Devv.Core.UniDataTier;

namespace BikeBouncer.BusinessLayer.Entities
{
	[DataContract, Serializable]
	public class Address : BaseEntity
	{
		#region Properties

		[DataMember]
		public Int32 UserId { get; set; }

		[DataMember]
		public City City { get; set; }

		[DataMember]
		public String Postcode { get; set; }

		[DataMember]
		public String Location { get; set; }

		[DataMember]
		public String Phone { get; set; }
		
		#endregion

		#region Internal use only properties

		public Double Latitude { get; set; }
		public Double Longitude { get; set; }

		#endregion

		#region Extra properties

		public Boolean HasContents
		{
			get
			{
				return Postcode.Length > 2 || Location.Length > 2 || Phone.Length > 2;
			}
		}

		#endregion

		#region Constructor

		public Address()
		{
			City = new City();
		}

		public Address(Int32 id)
		{
			Construct(DataAccess.Address_Get(id));
		}

		public Address(SqlDataReader reader)
		{
			Construct(reader);
		}

		#endregion

		#region Loading

		public override void Load(SqlDataReader reader)
		{
			Id = DataHandler.GetInt32(reader, "AddressId");
			UserId = DataHandler.GetInt32(reader, "UserId");
			Postcode = DataHandler.GetString(reader, "AddressPostcode");
			Location = DataHandler.GetString(reader, "AddressLocation");
			Phone = DataHandler.GetString(reader, "AddressPhone");
			Latitude = DataHandler.GetDouble(reader, "AddressLatitude");
			Longitude = DataHandler.GetDouble(reader, "AddressLongitude");
			Active = DataHandler.GetBoolean(reader, "AddressActive");

			City = new City();
			City.Load(reader);
		}

		#endregion

		#region ToString

		public new String ToString()
		{
			return ToString(Environment.NewLine);
		}

		public String ToString(String separator)
		{
			StringBuilder sb = new StringBuilder();

			if (!String.IsNullOrEmpty(Location))
			{
				sb.Append(Location + separator);
			}

			if (!String.IsNullOrEmpty(Postcode))
			{
				sb.Append(Postcode + separator);
			}

			sb.Append(City.ToString(separator));

			return sb.ToString();
		}

		#endregion
	}
}